package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.model.CommunicationRecord;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.model.DeviceSendMsgState;
import com.firefightingiot.firefightingiot.model.SendMsgState;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import com.firefightingiot.firefightingiot.support.utils.requestutil.HttpRequestUtil;
import com.firefightingiot.firefightingiot.web.mapper.CommunicationRecordMapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceSendMsgStateMapper;
import com.firefightingiot.firefightingiot.web.mapper.SendMsgStateMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yang yu
 * @version 1.0
 * @date 2020/8/26 15:32
 */
@Service
public class SendMsgStateService extends BaseService {

    @Resource
    public MyLogger logger;
    @Resource
    private SendMsgStateMapper sendMsgStateMapper;
    @Resource
    private DeviceSendMsgStateMapper deviceSendMsgStateMapper;
    @Resource
    private CommunicationRecordMapper communicationRecordMapper;
    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;
    /**
     * 语音短信状态列表
     * @param jsonParam
     * @return
     */
    public RespEntity getSendMsgStateList(JSONObject jsonParam) {
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //单个设备状态表
        List<DeviceSendMsgState> deviceSendMsgStateList = deviceSendMsgStateMapper.selectList(new QueryWrapper<DeviceSendMsgState>().eq("number", jsonParam.getStr("number")));

        if (deviceSendMsgStateList.isEmpty()) {
            //查询状态默认值
            List<SendMsgState> sendMsgStates = sendMsgStateMapper.selectList(new QueryWrapper<SendMsgState>().eq("device_classify",jsonParam.getStr("deviceClassify")));
            return RespEntity.success(sendMsgStates);
        }
        return RespEntity.success(deviceSendMsgStateList);
    }

    /**
     * 设备语音短信状态新增
     * @param jsonParam
     * @return
     */
    public RespEntity insertSendMsgState(JSONObject jsonParam) {
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        if (jsonParam.getStr("number")==null|| StrUtil.isBlank(jsonParam.getStr("number"))){
            return RespEntity.fail("设备号不能为空!");
        }
        DeviceSendMsgState deviceSendMsgState = new DeviceSendMsgState();
        deviceSendMsgState.setDeviceClassify(jsonParam.getStr("deviceClassify"));
        switch (jsonParam.getStr("deviceClassify")) {
            case "1":
                //电气火灾
                deviceSendMsgState.setName("电气火灾");
                break;
            case "2":
                //消防用水
                deviceSendMsgState.setName("消防用水");
                break;
            case "3":
                //烟感
                deviceSendMsgState.setName("烟感");
                break;
            case "4":
                //可燃气体
                deviceSendMsgState.setName("可燃气体");
                break;
            case "5":
                //火灾自动联网
                deviceSendMsgState.setName("火灾自动联网");
                break;
            case "6":
                //消防可视化
                deviceSendMsgState.setName("慧眼识别");
                break;
            case "7":
                //智能灭火器监测系统
                deviceSendMsgState.setName("智能灭火器监测系统");
                break;
            case "10":
                //水泵监测系统
                deviceSendMsgState.setName("水泵监测系统");
                break;
            case "11":
                //智慧加油站
                deviceSendMsgState.setName("智慧加油站");
                break;
            case "12":
                //智慧梯控
                deviceSendMsgState.setName("智慧梯控");
                break;
            case "13":
                //智慧水域
                deviceSendMsgState.setName("智慧水域");
                break;
            case "14":
                //公共卫生
                deviceSendMsgState.setName("公共卫生");
                break;
            case "16":
                //明厨亮灶
                deviceSendMsgState.setName("明厨亮灶");
                break;
            case "17":
                //综合治理可视化
                deviceSendMsgState.setName("综合治理可视化");
                break;
        }
        deviceSendMsgState.setMsgState(jsonParam.getInt("msgState"));
        deviceSendMsgState.setMsgInterval(jsonParam.getStr("msgInterval"));
        deviceSendMsgState.setNumber(jsonParam.getStr("number"));
        deviceSendMsgState.setVoiceState(jsonParam.getInt("voiceState"));
        deviceSendMsgState.setVoiceInterval(jsonParam.getStr("voiceInterval"));
        deviceSendMsgStateMapper.delete(new QueryWrapper<DeviceSendMsgState>().eq("number",jsonParam.getStr("number")));
        deviceSendMsgStateMapper.insert(deviceSendMsgState);
        return RespEntity.success();
    }


    /**
     * @Author YangYu
     * @Description
     * @Date Created in 17:20 2022/5/5
     * @Param [jsonParam]
     */

    public RespEntity getCommunicationRecordListPage(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        Page<CommunicationRecord> page = new Page<>(jsonParam.getInt("page"), jsonParam.getInt("limit"));
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userType = token.getString("user_type");

        IPage<CommunicationRecord> pageList = communicationRecordMapper.getCommunicationRecordListPage(page, jsonParam);
        Map<String, Object> data = new HashMap<>();
        data.put("totalSize", pageList.getTotal());//总条数
        data.put("pageSize", pageList.getSize());//每页展示多少条
        data.put("currentPage", pageList.getCurrent());//当前页
        data.put("Items", pageList.getRecords());//数据
        data.put("pages", pageList.getPages());//总页数
        data.put("currentSize", pageList.getRecords().size());//当前页多少条
        return RespEntity.success(data);
    }

    /**
     * @Author YangYu
     * @Description 企业一键开启短信或语音
     * @Date Created in 9:45 2022/5/16
     * @Param [jsonParam]
     */

    public RespEntity oneClickOnOrOff(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        List<String> list = com.alibaba.fastjson.JSONObject.parseArray(jsonParam.getJSONArray("deviceClassify").toString(), String.class);
        QueryWrapper<DeviceBusiness> qw = new QueryWrapper<>();
        qw.in("device_classify",list);
        qw.eq("business_id",jsonParam.getStr("businessId"));
        //本单位所有符合的设备
        List<DeviceBusiness> deviceBusinessList = deviceBusinessMapper.selectList(qw);
        //设备号
        List<String> numbers = new ArrayList<>();
        //状态数据
        List<DeviceSendMsgState> deviceSendMsgStateList = new ArrayList<>();

        for (DeviceBusiness deviceBusiness : deviceBusinessList){
            numbers.add(deviceBusiness.getNumber());
        }
        QueryWrapper<DeviceSendMsgState> qws = new QueryWrapper<>();
        qws.in("number",numbers);
        //更新数据用
        List<DeviceSendMsgState> deviceSendMsgStatesList = deviceSendMsgStateMapper.selectList(qws);
        for (DeviceBusiness deviceBusiness : deviceBusinessList){
            DeviceSendMsgState deviceSendMsgState = new DeviceSendMsgState();
            deviceSendMsgState.setDeviceClassify(deviceBusiness.getDeviceClassify());
            switch (deviceBusiness.getDeviceClassify()) {
                case "1":
                    //电气火灾
                    deviceSendMsgState.setName("电气火灾");
                    break;
                case "2":
                    //消防用水
                    deviceSendMsgState.setName("消防用水");
                    break;
                case "3":
                    //烟感
                    deviceSendMsgState.setName("烟感");
                    break;
                case "4":
                    //可燃气体
                    deviceSendMsgState.setName("可燃气体");
                    break;
                case "5":
                    //火灾自动联网
                    deviceSendMsgState.setName("火灾自动联网");
                    break;
                case "6":
                    //消防可视化
                    deviceSendMsgState.setName("慧眼识别");
                    break;
                case "7":
                    //智能灭火器监测系统
                    deviceSendMsgState.setName("智能灭火器监测系统");
                    break;
                case "10":
                    //水泵监测系统
                    deviceSendMsgState.setName("水泵监测系统");
                    break;
                case "11":
                    //智慧加油站
                    deviceSendMsgState.setName("智慧加油站");
                    break;
                case "12":
                    //智慧梯控
                    deviceSendMsgState.setName("智慧梯控");
                    break;
                case "13":
                    //智慧水域
                    deviceSendMsgState.setName("智慧水域");
                    break;
                case "14":
                    //公共卫生
                    deviceSendMsgState.setName("公共卫生");
                    break;
                case "16":
                    //明厨亮灶
                    deviceSendMsgState.setName("明厨亮灶");
                    break;
                case "17":
                    //综合治理可视化
                    deviceSendMsgState.setName("综合治理可视化");
                    break;
            }
            deviceSendMsgState.setNumber(deviceBusiness.getNumber());
            //type = 1 一键开启  type = 2 一键关闭
            if (jsonParam.getStr("type").equals("1")){
                //开启短信
                if (jsonParam.getStr("state").equals("1")){
                    deviceSendMsgState.setMsgState(1);
                    if (deviceSendMsgStatesList.size() > 0){
                        for (DeviceSendMsgState deviceSendMsgState1 : deviceSendMsgStatesList){
                            if (deviceSendMsgState1.getNumber().equals(deviceBusiness.getNumber())){
                                deviceSendMsgState.setVoiceState(deviceSendMsgState1.getVoiceState());
                                break;
                            }else {
                                deviceSendMsgState.setVoiceState(2);
                            }
                        }
                    }else {
                        deviceSendMsgState.setVoiceState(2);
                    }
                    //开启语音
                } else if (jsonParam.getStr("state").equals("2")){
                    deviceSendMsgState.setVoiceState(1);
                    if (deviceSendMsgStatesList.size() > 0){
                        for (DeviceSendMsgState deviceSendMsgState1 : deviceSendMsgStatesList){
                            if (deviceSendMsgState1.getNumber().equals(deviceBusiness.getNumber())){
                                deviceSendMsgState.setMsgState(deviceSendMsgState1.getMsgState());
                                break;
                            }else {
                                deviceSendMsgState.setMsgState(2);
                            }
                        }
                    }else {
                        deviceSendMsgState.setMsgState(2);
                    }
                    //开启短信/语音
                }else {
                    deviceSendMsgState.setMsgState(1);
                    deviceSendMsgState.setVoiceState(1);
                }

            }else {
                //关闭短信
                if (jsonParam.getStr("state").equals("1")){
                    deviceSendMsgState.setMsgState(2);
                    if (deviceSendMsgStatesList.size() > 0){
                        for (DeviceSendMsgState deviceSendMsgState1 : deviceSendMsgStatesList){
                            if (deviceSendMsgState1.getNumber().equals(deviceBusiness.getNumber())){
                                deviceSendMsgState.setVoiceState(deviceSendMsgState1.getVoiceState());
                                break;
                            }else {
                                deviceSendMsgState.setVoiceState(2);
                            }
                        }
                    }else {
                        deviceSendMsgState.setVoiceState(2);
                    }

                    //关闭语音
                } else if (jsonParam.getStr("state").equals("2")){
                    deviceSendMsgState.setVoiceState(2);
                    if (deviceSendMsgStatesList.size() > 0){
                        for (DeviceSendMsgState deviceSendMsgState1 : deviceSendMsgStatesList){
                            if (deviceSendMsgState1.getNumber().equals(deviceBusiness.getNumber())){
                                deviceSendMsgState.setMsgState(deviceSendMsgState1.getMsgState());
                                break;
                            }else {
                                deviceSendMsgState.setMsgState(2);
                            }
                        }
                    }else {
                        deviceSendMsgState.setMsgState(2);
                    }
                    //关闭短信/语音
                }else {
                    deviceSendMsgState.setMsgState(2);
                    deviceSendMsgState.setVoiceState(2);
                }
            }
            deviceSendMsgStateList.add(deviceSendMsgState);
        }
        if (numbers.size() >0){
            deviceSendMsgStateMapper.batchDelete(numbers);
            deviceSendMsgStateMapper.insertZB(deviceSendMsgStateList);
        }
        return RespEntity.success();
    }

}
